package pl.poznan.put.cs.idss.students.kingcart.ui;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.googlecode.javacv.cpp.opencv_videostab.VideoFileSource;

import pl.poznan.put.cs.idss.students.kingcart.i18n.Messages;
import pl.poznan.put.cs.idss.students.kingcart.properties.Properties;

public class Toolbox extends JPanel implements ActionListener {

	private static final long serialVersionUID = 6716778850902902694L;

	Logger log = Logger.getLogger("pl.poznan.put.cs.idss.students.coolring");

	private List<JButton> buttonsList = new ArrayList<JButton>();

	private int posX;
	private int posY;

	private GeneralWindow parentFrame;

	private JComboBox sourceList;

	private JButton openButton;

	private JButton resumeButton;

	public Toolbox(GeneralWindow parent) {

		parentFrame = parent;

		setLayout(new FlowLayout(FlowLayout.LEADING));

		setSize(Properties.number("toolbox_width"),
				Properties.number("toolbox_height"));

		posX = Properties.number("toolbox_location_x");
		posY = Properties.number("toolbox_location_y");
		setLocation(posX, posY);

		setVisible(true);

		String[] sourceStrings = {"Video", "Webcam" };
		sourceList = new JComboBox(sourceStrings);
		sourceList.setSelectedIndex(0);
		sourceList.addActionListener(this);
		add(sourceList);
		
		// TODO: move names to properties
		openButton = new JButton("Open file");
		openButton.setActionCommand("open");
		openButton.setEnabled(true);
		
		JButton startButton = new JButton("Pause");
		startButton.setActionCommand("pause");
		
		resumeButton = new JButton("Resume");
		resumeButton.setActionCommand("resume");
		resumeButton.setEnabled(false);

		buttonsList.add(openButton);
		buttonsList.add(startButton);
	//	buttonsList.add(resumeButton);

		drawElements();
	}

	private void drawElements() {
		int currPosY = posY; 
		for (JButton button : buttonsList) {
			button.setLocation(posX, currPosY);
			button.addActionListener(this);
			button.setVisible(true);
			button.setContentAreaFilled(false);
			button.setFocusPainted(false);
			add(button);
			currPosY += 100;
			System.out.println(button);
		}
	}

	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource().equals(sourceList)){
			changeSource();
			return;
		}
		//TODO move commands to properties
		String command = e.getActionCommand();
		if (command.equals("close")) {
			log.info("Closing application");
			parentFrame.dispose();
		} else if (command.equals("open")) {
			openFile();
		} 
		else if (command.equals("pause")){
			resumeButton.setEnabled(true);
			getSnapshot();
		}
		else if (command.equals("resume")){
			resumeButton.setEnabled(false);
			parentFrame.resume();
		}
		else {
			log.severe("Action " + e.getActionCommand() + " not supported!");
		}

	}
	private void changeSource() {
		if(sourceList.getSelectedIndex()  == 0){
			openButton.setEnabled(true);
			parentFrame.changeSource(0);
		}
		else{
			openButton.setEnabled(false);
			parentFrame.changeSource(1);
			
		}
		
	}

	private void getSnapshot() {
		parentFrame.snapshot();		
	}

	private void openFile() {
		JFileChooser chooser = new JFileChooser();
		File f = null;
		try {
			f = new File(new File(".").getCanonicalPath()+"\\src\\main\\resources\\samples\\");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    chooser.setCurrentDirectory(f);
		chooser.setDialogTitle(Messages
				.get("UI.application_choose_file_dialog"));
		//TODO move to properties
		FileFilter videoFilter = new FileNameExtensionFilter("Video files (*.avi; *.mp4)",
				"avi", "mp4");
		chooser.setFileFilter(videoFilter);
		if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			log.info("Choose file dialog; image: "
					+ chooser.getSelectedFile().getName());
			try {
				parentFrame.startVideoCapture(
						chooser.getSelectedFile().getAbsolutePath());
			//	parentFrame.setTitle(Messages.get("UI.application_name") + ": "
			//			+ chooser.getSelectedFile().getName());
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null,
						Messages.get("UI.error_msg_not_a_video"),
						Messages.get("UI.erro"),
						JOptionPane.ERROR_MESSAGE);
			}
		} else {
			log.info("Cannot open choosed file.");
		}
	}

}
